<%@ page language="java" pageEncoding="UTF-8"%>
<%@include file="/common/taglibs.jsp"%>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<title>添加部门</title>
		<link rel="stylesheet" type="text/css" href="${ctx }/css/caas_style.css" />
		<script type="text/javascript" src="${ctx }/js/jquery/jquery-1.3.min.js"></script>
		<script type="text/javascript" src="${ctx}js/jquery/lhgdialog/lhgcore.min.js"></script>
		<script type="text/javascript" src="${ctx}js/jquery/lhgdialog/lhgdialog.min.js"></script>
		<SCRIPT language=JavaScript>  
			jQuery(document).ready(function(){			
			    var dg = frameElement.lhgDG; 			   
			   			   
			    var param = dg.getArgs();
			    var pOrgCode = param.pOrgCode;
			    var operate = param.operate;
			    var orgId = param.orgid;
			    if(operate == 'add'){
			    	dg.addBtn("btn_confirm","确定",addOrg,"left");
			    }else if(operate == 'update'){
			    	init(orgId);
			    	dg.addBtn("btn_confirm","确定",addOrg,"left");
			    }else if(operate == 'show'){
			    	init(orgId);
			    	jQuery("#orgname").attr("readonly","readonly");					
					jQuery("#orgcode1").attr("readonly","readonly");
					jQuery("#remark").attr("readonly","readonly");	
			    }
			    
			    //确定按钮单击事件
				function addOrg(){				
					var url = operate == 'update'? "${ctx}/org/update":"${ctx}/org/add";
					var info = operate == 'update'?"修改":"添加";
					var param = {};
					var orgName = jQuery("#orgname").val();					
					var orgCode = jQuery("#orgcode1").val();
					var remark = jQuery("#remark").text();
					var flag = validate(orgName,orgCode,remark);
					if(!flag){
						return;
					}
					param.orgCode = orgCode;
					//检查部门编码是否已经存在
					jQuery.post("${ctx}/org/orgExists",param,function(data,textStatus,jqXHR){		 
						if(data.exists){
							alert("该组织机构编码已经存在，请重新输入!");
							jQuery("#orgcode1").focus();
							return;
						}else{					
							param = {};
							param["org.orgName"] = orgName;
							param["org.orgCode1"] = orgCode;
							param["org.remark"] = remark;
							if(operate == 'update'){
								param["org.id"] = orgId;
							}
							param.pOrgCode = pOrgCode;
							jQuery.post(url,param,function(data,textStatus,jqXHR){
								if(data.success){
									alert(info + "成功!");
								}else{
									alert(info + "失败!");
								}						
								dg.cancel();
							},'json');
						}						
					},'json');									
				}
				//end 确定按钮单击事件
				
				//初始化表单信息
				function init(_orgId){
					var param = {};
					param.orgId = _orgId;
					jQuery.post("${ctx}/org/findOrgById",param,function(data,textStatus,jqXHR){
						jQuery("#orgname").val(data.orgName);					
						jQuery("#orgcode1").val(data.orgCode1);
						jQuery("#remark").text(data.remark);						 
					},'json');									
				}				
				
				//检查页面元素输入的值是否合法
				function validate(orgName,orgCode,remark){
					if(orgName == null || orgName == ''){
						alert("部门名称不能为空!");
						jQuery("#orgname").focus();
						return false;
					}else if(orgName.length > 10){
						alert("部门名称不能超过10个字符!");
						jQuery("#orgname").focus();
						return false;
					}
					if(orgCode == null || orgCode == ''){
						alert("部门编码不能为空!");
						jQuery("#orgcode1").focus();
						return false;
					}else if(orgCode.length > 10){
						alert("部门编码不能超过10个字符!");
						jQuery("#orgcode1").focus();
						return false;
					}
					if(remark.length > 127){						
						alert("部门编码不能超过127个字符!");
						jQuery("#remark").focus();
						return false;
					}
					return true;
				}
				//end 检查页面元素输入的值是否合法
			});
		</SCRIPT>
	</head>

	<body>
		<table width="99%" border="0" cellspacing="0" cellpadding="0"
			align="center">
			<tr>
				<td>
					<div class="biaoge">
						<form action="${ctx}/org/add" method="post" name="mainform" id="mainform">
							<table width="100%" border="0" cellspacing="1" cellpadding="0">
								<tr>
									<td align="left" class="biaoge_bg01" colspan="2">
										部门详细信息
									</td>
								</tr>
								<tr>
									<td width="30%" align="right" class="bg04">
										部门名称：
									</td>
									<td width="70%" class="biaoge_bg5">
										<label>
											<input type="text" name="orgname" id="orgname" size="50" /><font color="red" size="4">*</font>
										</label>
									</td>
								</tr>
								<tr>
									<td class="bg04" align="right">
										部门编码：
									</td>
									<td align="left" class="biaoge_bg5">
										<label>
											<input type="text" class="required" size="50" id="orgcode1"
												name="orgcode1" value=""><font color="red" size="4">*</font>
										</label>
									</td>
								</tr>
								<tr>
									<td class="bg04" align="right">
										部门备注信息：
									</td>
									<td align="left" class="biaoge_bg5">
										<label>
											<textarea rows="4" cols="39" name="remark" id="remark"></textarea>
										</label>
									</td>
								</tr>
							</table>
						</form>
					</div>
				</td>
			</tr>
		</table>
</html>